/*
 * @(#)UserDAOImpl.java		       Project:SpikeSC
 * Date:2012-7-24
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.spikesc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.sinaapp.msdxblog.spikesc.dao.IUserDAO;
import com.sinaapp.msdxblog.spikesc.vo.SpikeUser;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class UserDAOImpl implements IUserDAO {
	private Connection mConnection;
	private PreparedStatement mStatement;

	public UserDAOImpl(Connection connection) {
		mConnection = connection;
	}

	@Override
	public SpikeUser findUser(String name, String password) throws SQLException {
		SpikeUser user = null;
		String sql = "SELECT t_spikeuser.id, username, password, rolename FROM t_role, t_spikeuser WHERE username = ? AND password = ? AND roleId=t_role.id";
		mStatement = mConnection.prepareStatement(sql);
		mStatement.setString(1, name);
		mStatement.setString(2, password);
		ResultSet resultSet = mStatement.executeQuery();
		if (resultSet.next()) {
			user = new SpikeUser();
			user.setId(resultSet.getInt(resultSet.findColumn("id")));
			user.setUsername(resultSet.getString(resultSet
					.findColumn("username")));
			user.setPassword(resultSet.getString(resultSet
					.findColumn("password")));
			user.setRoleName(resultSet.getString(resultSet
					.findColumn("rolename")));
		}
		return user;
	}

	@Override
	public boolean saveUser(String name, String password, int roleId) throws SQLException {
		String sql = "INSERT INTO t_spikeuser (username, password, roleId) value(?, ?, ?)";
		mStatement = mConnection.prepareStatement(sql);
		mStatement.setString(1, name);
		mStatement.setString(2, password);
		mStatement.setInt(3, roleId);
		return (mStatement.executeUpdate() >= 1);
	}

	@Override
	public boolean updateUser(String name, String password) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeUser(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int findUserCount() throws SQLException {
		String sql = "SELECT COUNT(*) FROM t_spikeuser";
		mStatement = mConnection.prepareStatement(sql);
		ResultSet resultSet = mStatement.executeQuery();
		if (resultSet.next()) {
			return resultSet.getInt(1);
		}
		return 0;
	}

}
